package thread;

class SingletonLazy1 {
    private static volatile SingletonLazy1 instance = null;
    private static Object locker = new Object();

    private SingletonLazy1() {

    }

    public static SingletonLazy1 getInstance() {
        //按需加锁，涉及到线程安全问题再加锁
        //第一个if判断的是否要加锁（时间效率）
        if (instance == null) {
            synchronized (locker) {
                //第二个if判断的是否要new一个
                if (instance == null) {
                    instance = new SingletonLazy1();
                }
            }
        }
        return instance;
    }
}


public class Demo57 {

    public static void main(String[] args) {

    }
}
